<script lang="ts" setup>
import { pageList } from "@/api/orgStaff";
import draggable from "vuedraggable";

const props = defineProps({
  modelValue: {
    type: Object,
    required: true,
  },
});

const store = useStore(),
  emits = defineEmits(["update:modelValue","change"]);

const form: any = ref({
    applyGroupList: [],
    businessParams: "",
    type: 1,
    submitResourceUrls: "",
  }),
  templateList = ref<Array<any>>([]),
  auditUserList = ref<Array<any>>([]);
const formRef = ref();

const userInfo = store.state.user.userInfo;

const rules = {
  submitTitle: [{ required: true, message: "请输入审核标题", trigger: "blur" }],
  submitResourceUrls: [
    { required: true, message: "请上传审核资料", trigger: "change" },
  ],
};

watch(
  () => props.modelValue,
  (val) => {
    nextTick(() => {
      form.value = Object.assign({}, form.value, val);
    });
  },
  { deep: true, immediate: true }
);

// 添加审核步骤
const addAuditStep = () => {
  form.value.applyGroupList.push({ groupType: "or" });
  onChange();
};

// 获取审核人员列表
const getAuditUser = async () => {
  const rs = await pageList({
    current: 1,
    size: -1,
    status: 1,
    name: "",
    insId: userInfo.insId,
  });
  if (rs.code == 200) {
    auditUserList.value = rs.payload;
  }
};

const validate = (callback: (valid: boolean) => {}) => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      if (form.value.type == 0) {
        form.value.applyGroupList = [];
      } else {
        form.value.templateId = "";
        form.value.applyGroupList.forEach((item: any, index: number) => {
          item.merchantUserIds = item.merchantUserId.join(",");
          item.sortNum = index + 1;
        });
      }
      onChange();
      callback(true);
    } else callback(false);
  });
};

// 删除审核流程
const handleDelete = (index: number) => {
  form.value.applyGroupList.splice(index, 1);
};

const resetFields = () => {
  nextTick(() => {
    if (formRef.value) formRef.value.resetFields();
  });
};

const onChange = () => {
  emits("update:modelValue", form.value);
  emits("change",form.value)
};

onMounted(() => {
  getAuditUser();
});

defineExpose({ validate, resetFields });
</script>
<template>
  <el-form
    :model="form"
    :rules="rules"
    ref="formRef"
    label-width="auto"
    :validate-on-rule-change="false"
  >
    <el-form-item label="审核标题" prop="submitTitle">
      <el-input
        type="text"
        placeholder="请输入审核标题"
        clearable
        v-model="form.submitTitle"
        maxlength="30"
        show-word-limit
        @change="onChange"
      >
      </el-input>
    </el-form-item>
    <el-form-item label="审核资料" prop="submitResourceUrls">
      <Upload
        v-model="form.submitResourceUrls"
        file-path="act-audit/"
        accept=""
        drag
        @change="onChange"
        style="width: 300px"
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">拖拽文件到这里或 <em>点击上传</em></div>
      </Upload>
    </el-form-item>
    <el-form-item label="审核信息" prop="submitRemark">
      <el-input
        type="textarea"
        placeholder="请输入审核信息"
        :autosize="{ minRows: 2, maxRows: 5 }"
        v-model="form.submitRemark"
        maxlength="100"
        show-word-limit
        @change="onChange"
      >
      </el-input>
    </el-form-item>
    <el-form-item label="审核流程" prop="type">
      <el-radio-group v-model="form.type" @change="onChange">
        <!-- <el-radio :label="0">模板</el-radio> -->
        <el-radio :label="1">自定义</el-radio>
      </el-radio-group>
    </el-form-item>
    <template v-if="form.type == 0">
      <el-form-item label="审核流程模板" prop="templateId">
        <el-select
          v-model="form.templateId"
          placeholder="请选择流程模板"
          @change="onChange"
        >
          <el-option
            v-for="item in templateList"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </template>
    <template v-else-if="form.type == 1">
      <el-form-item>
        <el-button icon="plus" @click="addAuditStep">添加审核步骤</el-button>
      </el-form-item>
      <div class="info" v-if="form.applyGroupList.length >= 2">
        <span>拖动行可调整顺序</span>
      </div>
      <draggable v-model="form.applyGroupList" group="people" @sort="onChange">
        <template #item="{ element, index }">
          <div class="progress-item">
            <el-card shadow="always">
              <div class="order" v-if="form.applyGroupList.length > 1">
                <div
                  class="line"
                  :style="{ opacity: index == 0 ? 0 : 1 }"
                ></div>
                <div class="num">{{ index + 1 }}</div>
                <div
                  class="line"
                  :style="{
                    opacity: index == form.applyGroupList.length - 1 ? 0 : 1,
                  }"
                ></div>
              </div>
              <el-form-item label-width="0" style="margin-bottom: 0">
                <el-form-item
                  label-width="0"
                  :prop="`applyGroupList.${index}.name`"
                  :rules="[
                    {
                      required: true,
                      message: '请输入流程标题',
                      trigger: 'blur',
                    },
                  ]"
                >
                  <el-input
                    type="text"
                    placeholder="流程标题"
                    clearable
                    maxlength="30"
                    :show-word-limit="true"
                    v-model="element.name"
                    @change="onChange"
                    style="width: 230px"
                  />
                </el-form-item>
                <el-form-item
                  label-width="10px"
                  :prop="`applyGroupList.${index}.groupType`"
                  :rules="[
                    {
                      required: true,
                      message: '请选择审核类型',
                      trigger: 'change',
                    },
                  ]"
                >
                  <el-select
                    v-model="element.groupType"
                    placeholder="审核类型"
                    style="width: 100px"
                    @change="onChange"
                  >
                    <el-option label="或签" value="or" />
                    <el-option label="并签" value="and" />
                    <el-option label="抄送" value="notify" />
                  </el-select>
                </el-form-item>
                <el-form-item
                  label-width="10px"
                  :prop="`applyGroupList.${index}.merchantUserId`"
                  :rules="[
                    {
                      required: true,
                      message: '请选择审核人',
                      trigger: 'change',
                    },
                  ]"
                >
                  <el-select
                    v-model="element.merchantUserId"
                    placeholder="审核人"
                    multiple
                    style="width: 220px"
                    @change="onChange"
                  >
                    <el-option
                      v-for="item in auditUserList"
                      :label="item.realName"
                      :value="item.id"
                    >
                      <span style="float: left">{{ item.realName }}</span>
                      <span
                        style="
                          float: right;
                          color: var(--el-text-color-secondary);
                          font-size: 13px;
                        "
                        >{{ item.insName }}</span
                      >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form-item>
              <el-popconfirm
                title="确认删除该条审核吗"
                @confirm="handleDelete(index)"
              >
                <template #reference>
                  <el-button type="text" title="删除" class="delete-btn">
                    <el-icon><CircleClose /></el-icon>
                  </el-button>
                </template>
              </el-popconfirm>
            </el-card>
          </div>
        </template>
      </draggable>
    </template>
  </el-form>
</template>
<style lang="scss" scoped>
.info {
  padding-left: 30px;
  margin-bottom: 10px;
  span {
    font-size: 13px;
    color: #909399;
    font-weight: normal;
  }
}
.progress-item {
  margin-bottom: 10px;
  padding-left: 30px;
  position: relative;
  cursor: move;
  .order {
    position: absolute;
    left: 0;
    top: -5px;
    bottom: -5px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .line {
      width: 1px;
      flex: 1;
      background-color: var(--el-color-primary);
    }
    .num {
      border-radius: 50%;
      text-align: center;
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 14px;
      border: 1px solid var(--el-color-primary);
      color: var(--el-color-primary);
    }
  }
  .delete-btn {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>
